package com.ujs.edu.medicine.controller;

import com.alibaba.fastjson.JSONObject;
import com.ujs.edu.medicine.dao.YunjiayiShopMapper;
import com.ujs.edu.medicine.dao.YunjiayiUserMapper;
import com.ujs.edu.medicine.dataobject.YunjiayiShop;
import com.ujs.edu.medicine.dataobject.YunjiayiUser;
import com.ujs.edu.medicine.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * @Author:
 * @Date: Created in 18:48 2020/9/21
 * Description: 注册
 * Modified By:
 */
@Controller
public class RegisterController {

    @Autowired
    private YunjiayiUserMapper yunjiayiUserMapper;
    @Autowired
    private YunjiayiShopMapper yunjiayiShopMapper;

    /*
     * 注册
     * */
    @RequestMapping("/api/user/enrollx")
    public Result register(@RequestBody JSONObject object) {
        Result result = new Result();
        YunjiayiUser user=new YunjiayiUser();

        user.setUsername(object.getString("username"));
        user.setRealname(object.getString("realname"));
        user.setPassword(object.getString("password"));
        user.setAvatar("http://cloud-medicine.youchen-blog.cn/avatar.jpg");

        switch (object.getInteger("identify_id")) {
            case 3: {
                user.setIdentityId(0);
                try {
                    yunjiayiUserMapper.insert(user);
                    result.setCode(0);
                    result.setMessage("插入患者成功");
                }catch (Exception e){
                    result.setCode(1);
                    result.setMessage("插入患者失败");
                }
            }
            break;

            case 4: {
                user.setIdentityId(0);
                user.setStatusID(1);
                try {
                    yunjiayiUserMapper.insert(user);
                    result.setCode(0);
                    result.setMessage("插入医生成功");
                }catch (Exception e){
                    result.setCode(1);
                    result.setMessage("插入医生失败");
                }
            }
            break;
            case 5: {
                user.setIdentityId(0);
                try {
                    int insershop=yunjiayiUserMapper.insert(user);
                    YunjiayiShop shop=new YunjiayiShop();

                    shop.setShopid(user.getUser_Id());
                    shop.setShopname(user.getUsername());
                    shop.setShopimage(user.getAvatar());

                    yunjiayiShopMapper.insert(shop);
                    result.setCode(0);
                    result.setMessage("插入药店成功");
                }catch (Exception e){
                    result.setCode(1);
                    result.setMessage("插入药店失败");
                }

            }
            break;
            case 6: {
                user.setIdentityId(0);
                try {
                    yunjiayiUserMapper.insert(user);
                    result.setCode(0);
                    result.setMessage("插入管理员成功");
                }catch (Exception e){
                    result.setCode(1);
                    result.setMessage("插入管理员失败");
                }
            }
            break;
            default: {
                result.setCode(0);
                result.setMessage("没有这个身份");
            }
            break;
        }

        return result;
    }
}
